package com.ruoyi.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.business.app.bo.AppMyCouponBo;
import com.ruoyi.business.app.vo.AppMyCouponVo;
import com.ruoyi.business.bo.HcxUserCouponAddBo;
import com.ruoyi.business.bo.HcxUserCouponEditBo;
import com.ruoyi.business.bo.HcxUserCouponQueryBo;
import com.ruoyi.business.domain.HcxUserCoupon;
import com.ruoyi.business.mapper.HcxUserCouponMapper;
import com.ruoyi.business.service.IHcxUserCouponService;
import com.ruoyi.business.vo.HcxUserCouponVo;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.AssertUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 用户所拥有的优惠劵Service业务层处理
 *
 * @author wsdz
 * @date 2022-08-18
 */
@Service
public class HcxUserCouponServiceImpl extends ServicePlusImpl<HcxUserCouponMapper, HcxUserCoupon> implements IHcxUserCouponService {

    @Override
    public HcxUserCouponVo queryById(Long id) {
        return getVoById(id, HcxUserCouponVo.class);
    }

    @Override
    public TableDataInfo<HcxUserCouponVo> queryPageList(HcxUserCouponQueryBo bo) {
        PagePlus<HcxUserCoupon, HcxUserCouponVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), HcxUserCouponVo.class);
        return PageUtils.buildDataInfo(result);
    }


    @Override
    public TableDataInfo<AppMyCouponVo> queryAppPageList(AppMyCouponBo bo) {
        PagePlus<HcxUserCoupon, AppMyCouponVo> result = pageVo(PageUtils.buildPagePlus(), buildAppQueryWrapper(bo), AppMyCouponVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<HcxUserCouponVo> queryList(HcxUserCouponQueryBo bo) {
        return listVo(buildQueryWrapper(bo), HcxUserCouponVo.class);
    }

    private LambdaQueryWrapper<HcxUserCoupon> buildQueryWrapper(HcxUserCouponQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HcxUserCoupon> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, HcxUserCoupon::getUserId, bo.getUserId());
        lqw.eq(bo.getCouponId() != null, HcxUserCoupon::getCouponId, bo.getCouponId());
        lqw.eq(StrUtil.isNotBlank(bo.getPhonenumber()), HcxUserCoupon::getPhonenumber, bo.getPhonenumber());
        lqw.like(StrUtil.isNotBlank(bo.getRealName()), HcxUserCoupon::getRealName, bo.getRealName());
        lqw.eq(StrUtil.isNotBlank(bo.getStatus()), HcxUserCoupon::getStatus, bo.getStatus());
        lqw.eq(StrUtil.isNotBlank(bo.getDelFlag()), HcxUserCoupon::getDelFlag, bo.getDelFlag());
        lqw.like(StrUtil.isNotBlank(bo.getName()), HcxUserCoupon::getName, bo.getName());
        lqw.like(bo.getBeginDate() != null, HcxUserCoupon::getBeginDate, bo.getBeginDate());
        lqw.like(bo.getEndDate() != null, HcxUserCoupon::getEndDate, bo.getEndDate());
        lqw.like(bo.getDenomination() != null, HcxUserCoupon::getDenomination, bo.getDenomination());
        lqw.like(bo.getConditionPrice() != null, HcxUserCoupon::getConditionPrice, bo.getConditionPrice());
        lqw.like(StrUtil.isNotBlank(bo.getUseIntroduce()), HcxUserCoupon::getUseIntroduce, bo.getUseIntroduce());
        return lqw;
    }

    private LambdaQueryWrapper<HcxUserCoupon> buildAppQueryWrapper(AppMyCouponBo bo) {
        LambdaQueryWrapper<HcxUserCoupon> lqw = Wrappers.lambdaQuery();
        lqw.eq(HcxUserCoupon::getUserId, SecurityUtils.getUserId());
        // 查询未使用
        if ("0".equals(bo.getStatus())) {
            lqw.eq(StrUtil.isNotBlank(bo.getStatus()), HcxUserCoupon::getStatus, bo.getStatus());
            lqw.apply("curdate() <= end_date ");
            lqw.apply("curdate() >= begin_date");
        } else if ("1".equals(bo.getStatus())) {
            // 查询已使用
            lqw.eq(StrUtil.isNotBlank(bo.getStatus()), HcxUserCoupon::getStatus, bo.getStatus());
        } else {
            // 查询过期的优惠券
            lqw.apply("curdate() > end_date ");
        }
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(HcxUserCouponAddBo bo) {
        HcxUserCoupon add = BeanUtil.toBean(bo, HcxUserCoupon.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(HcxUserCouponEditBo bo) {
        HcxUserCoupon update = BeanUtil.toBean(bo, HcxUserCoupon.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(HcxUserCoupon entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public String importHcxUserCoupon(List<HcxUserCouponVo> hcxUserCouponList) {
        AssertUtils.isTrue(CollectionUtils.isNotEmpty(hcxUserCouponList), "导入数据不能为空");
        int successNum = 0;
        for (HcxUserCouponVo hcxUserCoupon : hcxUserCouponList) {
            HcxUserCoupon add = BeanUtil.toBean(hcxUserCoupon, HcxUserCoupon.class);
            save(add);
            successNum++;
        }
        String msg = String.format("导入完成，共计 %d 条数据，成功 %d 条！", hcxUserCouponList.size(), successNum);
        if (successNum != hcxUserCouponList.size()) {
            msg = msg + String.format("失败 %d 条！", hcxUserCouponList.size() - successNum);
        }
        return msg;
    }
}
